package com.example.text.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.text.dao.AdminRolePermissionDao;
import com.example.text.entity.AdminPermission;
import com.example.text.entity.AdminRolePermission;
import com.example.text.service.AdminRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdminRolePermissionServiceImpl extends ServiceImpl<AdminRolePermissionDao, AdminRolePermission> implements AdminRolePermissionService {

    @Autowired
    AdminRolePermissionDao adminRolePermissionDao;

   public List<AdminRolePermission> findAllByRid(int rid){

       return adminRolePermissionDao.selectList(new QueryWrapper<AdminRolePermission>().eq("rid", rid));

    }

    @Transactional
    public void savePermChanges(int rid, List<AdminPermission> perms) {

        adminRolePermissionDao.delete(new QueryWrapper<AdminRolePermission>().eq("rid",rid));
        if(perms !=null) {
            perms.forEach(p -> {
                AdminRolePermission rp = new AdminRolePermission();
                rp.setRid(rid);
                rp.setPid(p.getId());
                adminRolePermissionDao.insert(rp);
            });
        }

    }
}
